<template>
  <div>
    <div class="class-header">
      <div class="header-nav">
        <div class="header-nav-left">
          <router-link to="/">
            <img
              src="../img/logo.png"
              alt=""
            >
            <span>80教育商城</span>
          </router-link>
        </div>
        <div class="header-nav-right">
          <ul class="header-nav-right-list">
            <router-link to="/Search">
              <li><i class="iconfont icon-search"></i> </li>
              <li><span class="search">搜索</span></li>
            </router-link>
            <li><span>会员中心</span></li>
          </ul>
        </div>
      </div>
    </div>

    <div class="class-content-wp">
      <div class="class-title-wp">
        <div>
          {{list[0].type == '注册公用设备工程师' ? '注册公用设备工程师' : list[0].type == '注册基础类' ? '注册基础类': ''}}
        </div>
      </div>

      <div class="class-list-wp">
        <div class="class-content-filter">
          <div>名称<i class="iconfont icon-play_fill"></i></div>
          <div>时间<i class="iconfont icon-play_fill"></i></div>
          <div>价格<i class="iconfont icon-play_fill"></i></div>
          <div>筛选<i class="iconfont icon-play_fill"></i></div>
        </div>
      </div>
      <div
        class="class-content-booklist"
        v-for="(list,index) in list"
        :key="index"
      >
        <!-- <router-link :to="`/BookDetail/${list.id}`"> -->
        <div class="class-content-book">
          <div class="class-content-book-img">
            <img
              src="../img/book1.jpg"
              alt=""
            >
          </div>
          <div class="tableCell tableCell-70">
            <router-link :to="`/BookDetail/${list.id}`">
              <div class="nameWrap">
                <p class="paramName">{{list.name}}</p>
              </div>
              <p class="productParam paramWrap ">类型：{{list.leixing}}</p>
              <p class="productParam paramWrap ">材质：{{list.caizhi}}</p>
              <p class="productParam paramWrap ">销量：{{list.xiaoliang}}</p>
            </router-link>
            <p class="productParam paramPrice paramWrap ">
              <span class="mallPrice">价格：￥ {{list.price}}</span>
              <span
                class="iconfont icon-publishgoods_fill shop-car"
                @click.stop="shopcarFn(list.id)"
              ></span>
            </p>
          </div>
        </div>
        <!-- </router-link> -->
      </div>

      <div class="pagenation ">
        <div class="pagePrev">&lt;</div>
        <div class="pagenationSelectDiv">
          <select class="pageSelect">
            <option class="pageOptions">1&nbsp;&nbsp;/&nbsp;&nbsp;4</option>
            <option class="pageOptions">2&nbsp;&nbsp;/&nbsp;&nbsp;4</option>
            <option class="pageOptions">3&nbsp;&nbsp;/&nbsp;&nbsp;4</option>
            <option class="pageOptions">4&nbsp;&nbsp;/&nbsp;&nbsp;4</option>
          </select>
        </div>
        <div class="pageNext">&gt;</div>
      </div>
    </div>
    <!-- 返回顶部 -->
    <div class="backTop">
      <span
        class="iconfont icon-packup return-top"
        style="color:#3285ff;background-color:#ccc"
      ></span>
    </div>
    <!-- 加入购物车遮罩 -->
    <div
      class="shopcar-blog"
      v-show="isShow"
    >
      <div class="shop-car-content-wp">
        <div
          class="shop-car-content-top"
          v-for="(list,index) in shopList"
          :key="index"
        >
          <div class="shop-car-img-wp">
            <img
              src="../img/book1.jpg"
              alt=""
              style="width:100%"
            >
          </div>
          <div class="shop-car-goodsname-wp">
            <div class="shop-car-goodsname">
              {{list.name}}
            </div>
            <div class="shop-car-price">
              ￥ {{ list.price }}.00
            </div>
          </div>
          <div
            class="iconfont icon-delete_fill shop-car-close"
            @click="closeFn"
          ></div>
          <div class="shop-car-count">
            <span>购买数量</span>
            <div class="shop-car-count-text">
              <span @click="changeNum(2)">-</span>
              <input
                type="text"
                v-model="count"
                @change="changeNum(1)"
              >
              <span @click="changeNum(0)">+</span>
            </div>
          </div>
        </div>
        <div class="shop-car-content-bottom">
          <div
            class="shop-car-btn"
            @click="joinShopCar"
          >
            加入购物车
          </div>
          <div
            class="shop-car-btn"
            style="background-color: #3285ff;"
            @click="nowBuy"
          >
            立即购买
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import session from "../util/session";
import { createNamespacedHelpers } from "vuex";
const { mapActions } = createNamespacedHelpers("home");
export default {
  data() {
    return {
      list: [{ type: "" }],
      isShow: false,
      count: 1,
      shopList: [],
      allArr: [],
      bianliang: true,
      currentId: ""
    };
  },
  mounted() {
    this.getClassDetailData(this.$route.params.type).then(
      data => (this.list = data)
    );
  },
  methods: {
    ...mapActions([
      "getClassDetailData",
      "getList",
      "getShopList",
      "postShopList",
      "updateShopList",
      "postShop"
    ]),
    shopcarFn(id) {
      this.currentId = id;
      this.count = 1;
      if (session.get().id) {
        this.getList().then(data => {
          this.allArr = data;
          this.shopList = data.filter(k => {
            return k.id == id;
          });
        });
        this.isShow = true;
      } else {
        alert("请登录！！");
      }
    },
    //关闭弹出的购物遮罩层
    closeFn() {
      this.isShow = false;
    },
    //点击购买
    nowBuy() {
      let id = this.currentId;
      let obj = {};
      let params = {};
      for (var i = 0; i < this.list.length; i++) {
        if (id == this.list[i].id) {
          obj = this.list[i];
        }
      }
      params.uid = session.get().id;
      params.goodsid = obj.id;
      params.goodsname = obj.name;
      params.price = obj.price;
      params.num = this.count;
      this.postShop(params).then(data => {
        let array = [];
        array.push(data);
        this.$router.push({ name: "ordercommit", params: { id: array } });
      });
    },
    //加入购物车
    joinShopCar() {
      let id = this.currentId;
      this.bianliang = true;
      let obj = {};
      for (var j = 0; j < this.allArr.length; j++) {
        if (id == this.allArr[j].id) {
          obj.goodsname = this.allArr[j].name;
          obj.goodsid = this.allArr[j].id;
          obj.price = this.allArr[j].price;
        }
      }
      obj.uid = session.get().id;
      obj.num = this.count;
      this.getShopList(session.get().id).then(data => {
        if (data.length == 0) {
          this.postShopList(obj);
        } else {
          let arr = [];
          for (var i = 0; i < data.length; i++) {
            arr.push(data[i].goodsid);
          }
          if (arr.includes(id)) {
            this.getShopList(session.get().id).then(res => {
              let arr = [];
              arr = res.filter(k => {
                return k.goodsid == id;
              });
              this.updateShopList({ id: arr[0].id, num: this.count });
            });
          } else {
            this.postShopList(obj);
          }
        }
        this.isShow = false;
      });
    },
    //改变商品数量
    changeNum(add, id) {
      if (add == 0) {
        this.count++;
      } else if (add == 1) {
        if (this.count <= 1) {
          this.count = 1;
        }
      } else {
        if (this.count > 1) {
          this.count--;
        }
      }
    }
  }
};
</script>

<style scoped>
.shopcar-blog {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.3);
  z-index: 99;
}
.shop-car-content-wp {
  min-height: 205px;
  max-height: 350px;
  background-color: #fff;
  position: absolute;
  bottom: 0;
  width: 100%;
}
.shop-car-content-bottom {
  position: absolute;
  bottom: 0;
  width: 100%;
  height: 40px;
  line-height: 1;
  border-top-color: #ebebeb;
  font-size: 0;
  overflow: hidden;
}
.shop-car-content-top {
  height: 100px;
  width: 100%;
  border-bottom: 1px solid #f0f0f0;
}
.shop-car-btn {
  float: left;
  width: 50%;
  height: 40px;
  line-height: 40px;
  background: #56c4e7;
  padding: 0;
  border: 0;
  margin: 0;
  color: #fff;
  font-size: 12px;
  text-align: center;
}
.shop-car-img-wp {
  width: 110px;
  height: 110px;
  line-height: 160px;
  position: absolute;
  left: 10px;
  background-color: #fff;
  border-radius: 5px;
  border: 1px solid #ececec;
  padding: 4px;
  top: -20px;
}
.shop-car-goodsname-wp {
  min-height: 100px;
  position: relative;
  left: 120px;
  max-width: 60%;
}
.shop-car-goodsname {
  width: 168px;
  height: 43px;
  font-size: 13px;
  word-break: break-all;
  white-space: normal;
  overflow: hidden;
  text-overflow: ellipsis;
  -webkit-box-orient: vertical;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  color: #333;
  line-height: 16px;
  padding: 9px 10px 6px;
}
.shop-car-price {
  font-size: 12px;
  color: #3285ff;
  padding: 6px 8px;
}
.shop-car-close {
  position: absolute;
  top: 0;
  right: 0;
  font-size: 24px;
  color: #777;
  padding: 5px;
}
.shop-car-count {
  height: 60px;
  line-height: 60px;
  font-size: 13px;
  position: relative;
  padding-left: 10px;
}
.shop-car-count-text {
  display: inline-block;
  float: right;
  position: relative;
  left: -7%;
}
.shop-car-count-text input {
  height: 25px;
  width: 32px;
  outline: none;
  text-align: center;
  border: none;
}
.shop-car-count-text span {
  background-color: #f5f5f5;
  display: inline-block;
  width: 32px;
  height: 27px;
  line-height: 27px;
  text-align: center;
  font-size: 16px;
  color: #333;
}

.tableCell-70 {
  width: 70%;
}
.tableCell {
  display: table-cell;
  vertical-align: middle;
  padding: 0 0;
  padding-left: 10px;
}
.paramName {
  font-size: 13px;
  color: #333;
  margin-bottom: 4px;
  text-overflow: ellipsis;
  overflow: hidden;
  padding-right: 5px;
  line-height: 17px;
}
.productParam {
  font-size: 11px;
  color: #666;
  line-height: 17px;
}
.paramWrap {
  padding-right: 10px;
}
.mallPrice {
  color: #3285ff;
  word-wrap: break-word;
}
.class-content-book {
  display: flex;
  margin: 5px 0;
  padding: 10px 0;
  border-top: 1px solid #f0f0f0;
}
.class-content-book-img {
  width: 30%;
  padding-left: 10px;
}
.class-content-book-img img {
  width: 100%;
  position: relative;
  top: 12px;
}
.shop-car {
  float: right;
  font-size: 20px;
  color: #3285ff;
}
.class-content-wp {
  margin-top: 8px;
  background-color: #fff;
  margin-bottom: 50px;
}
.class-title-wp {
  height: 45px;
  line-height: 45px;
  background: #fff;
  border-bottom: 1px solid #f0f0f0;
  color: #3285ff;
  font-size: 14px;
  padding-left: 12px;
}
.class-content-filter {
  display: flex;
  height: 24px;
  line-height: 24px;
  padding: 3px 15px;
  font-size: 14px;
  color: #636363;
  text-align: center;
}
.class-content-filter .icon-play_fill {
  color: #636363;
  font-size: 12px;
  position: absolute;
  transform: rotate(90deg);
}
.class-content-filter > div {
  flex-grow: 1;
  position: relative;
  height: 24px;
}
.pagenation {
  font-size: 14px;
  color: #333;
  height: 50px;
  width: 100%;
  text-align: center;
  position: relative;
  line-height: 50px;
  background-color: #fff;
}
.pagenation .pageNext,
.pagenation .pagePrev {
  width: 26px;
  height: 26px;
  background: #3285ff;
  border: 0;
  border-radius: 50%;
  overflow: hidden;
  background-size: 100%;
  line-height: 21px;
  color: #fff;
  position: absolute;
  top: 15px;
}
.pagenation .pagePrev {
  left: 105px;
}
.pagenation .pageNext {
  left: 190px;
}
.pageSelect {
  width: 72px;
  height: 25px;
  line-height: 1.05rem;
  font-size: 12px;
  border: none;
  background: 0 0;
  border-radius: 20px;
  position: relative;
  left: 10px;
  padding: 0 7px;
  margin-bottom: 10px;
  background: #fff;
  font-weight: 400;
  margin-top: 12px;
  text-indent: 0;
}
</style>
